package com.ehualu.check;

import com.ehualu.check.utils.CheckThread;
import com.ehualu.check.utils.MysqlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;

import static java.util.concurrent.Executors.*;

/**
 * @Author: 吴敬超
 * @Date: 2021/3/19 11:07
 */
public class CheckFileThreadPool {


    private static final Logger LOG = LoggerFactory.getLogger(CheckFileThreadPool.class);

    public static void main(String[] args) {

//        计算cpu核数，取cpu 的1/3（向上取整）为线程数
        double cpunum = Runtime.getRuntime().availableProcessors();


        LOG.info("服务器cpu核数：" + cpunum);

        int cpuceilNum = (int) Math.ceil(cpunum / 3);
        LOG.info("本服务占用核数：" + cpuceilNum);

        MysqlUtil mysqlUtil = new MysqlUtil();
        List<Map<String, String>> checkInfoList = mysqlUtil.getCheckinfo();


        ExecutorService es = newFixedThreadPool(cpuceilNum);
        for (int i = 0; i < checkInfoList.size(); i++) {
//            es.submit(new CheckThread(checkInfoList.get(i).get("id"), checkInfoList.get(i).get("ext_path"), checkInfoList.get(i).get("ext_hash_mothod")));
            es.submit(new CheckThread(checkInfoList.get(i).get("id"), checkInfoList.get(i).get("ext_path")));

        }
        // 关闭线程池:
        es.shutdown();
    }


}

